/**
 * 
 */
package gmu.drr.modelInst.simple;

import java.util.Iterator;

import gmu.drr.entity.ModelModel;
import gmu.drr.entity.User;
import gmu.drr.entity.UserGroup;
import gmu.drr.entity.UserGroupRelModel;

/**
 * @author RothmD01
 *
 */
public class UserGroupRelSimple extends UserGroupRelModel {
	private float groupWt[];

	public float[] getGroupWt() {
		return groupWt;
	}

	public void setGroupWt(float[] groupWt) {
		this.groupWt = groupWt;
	}

	/* end getters and setters */
	
	@Override
	public User getInverseUser(UserGroup group) {
		int count = allActive.size();
		float cum[] = new float[ count ];
		float sum = 0;
		int itCt = 0;
		int ind[] = new int[ count ];
		double rand = Math.random();
		User retVal = null;
		
		if( group == null) return null;
		
		for( Iterator<UserGroupRelModel> i = allActive.iterator(); i.hasNext();) {
			UserGroupRelSimple u;
			u = (UserGroupRelSimple)i.next();
			if( itCt > 0 ) cum[itCt] = cum[itCt-1] + (u.getGroupWt())[group.getContextInd()];
			ind[itCt] = u.getUserInd();
			itCt += 1;
		}
		sum = cum[count-1];
		sum *= rand;
		for( int i = 0; i < count; i++) {
			if( cum[i]>0) {
				itCt = i-1;
				break;
			}
		}
		retVal = allActive.get(itCt).getUser();
		
		return retVal;
	}

	public UserGroupRelSimple( ModelModel model ) {
		super();
		groupWt = new float[ model.getKu() ];
	}
	
	public UserGroupRelSimple() {
	}
}
